﻿using FluentValidation;
using Logistics.Web.Models.Order;
using Nop.Core.Domain.Logistics;
using Nop.Services.Localization;
using Nop.Services.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Logistics.Web.Validators
{
    public class OrderLocationValidator : AbstractValidator<OrderLocationModel>
    {
        public OrderLocationValidator(ILocalizationService localizationService, LogisticsInformationSettings logisticsInformationSettings, IPermissionService permissionService)
        {
            RuleFor(x => x.PackageNumber)
                .NotNull()
                .WithMessage(localizationService.GetResource("Logistics.Order.Fields.PackageNumber.Required"));

            RuleFor(x => x.PackageNumber)
                .GreaterThan(0)
                .WithMessage(localizationService.GetResource("Logistics.Order.Fields.PackageNumber.Invalid"));

            var maxPkg = permissionService.Authorize(StandardPermissionProvider.LogisticsVolumeShipping) ?
                logisticsInformationSettings.VolumeMaxPackagePerLocation :
                logisticsInformationSettings.MaxPackagePerLocation;
                        
            RuleFor(x => x.PackageNumber)
                .LessThanOrEqualTo(maxPkg)
                .WithMessage(String.Format(localizationService.GetResource("Logistics.Order.Fields.PackageNumber.Exceed"), maxPkg));

            RuleFor(x => x.NoSignature)
                .NotNull()
                .When(x => x.Address.Type == Nop.Core.Domain.Common.AddressType.Residential)
                .WithMessage(localizationService.GetResource("Logistics.Order.Fields.Signature.Required"));

            RuleFor(x => x.AdditionalInstruction)
                .Length(0, logisticsInformationSettings.MaxCommentLength)
                .When(x => x.AdditionalInstruction != null)
                .WithMessage(String.Format(localizationService.GetResource("Logistics.Order.Fields.AdditionalInstruction.Exceed"), logisticsInformationSettings.MaxCommentLength));
        }
    }
}